//
//  LoginCreateAccountViewController.swift
//  XGamesLive
//
//  Created by ios on 2024/5/14.
//

import UIKit

class LoginCreateAccountViewController: UIViewController,CustomNavgationBar{
    lazy var hudView: XGL_CustomHudView = XGL_CustomHudView()
    var createView: CreateAccountView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.createCustomBackgroundView()
        self.createView = CreateAccountView().then({ make in
            make.delegate = self
        })
        self.view.addSubview(self.createView)
        self.createView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.creatClearNavigationBar(title: "Create Account")
    }
    
}

extension LoginCreateAccountViewController{
    func verificationEmailRequest(){
        PanRequestManager<XGL_RequestBaseResponse<Response>,XGL_CustomRequestError>().request(LoginRequestAPI(requestAPI: .VerificationEmail(email: self.createView.email))) { result in
            switch result{
            case .success:
                self.registerEmailRequest()
                break
            default:
                break
            }
        }
    }
    
    func registerEmailRequest(){
        PanRequestManager<XGL_RequestBaseResponse<LoginData>,XGL_CustomRequestError>().request(LoginRequestAPI(requestAPI: .RegisterWithEmail(email: self.createView.email, password: self.createView.password))) { result in
            self.hudView.dismiss()
            switch result{
            case .success(let response):
                if let result = response.resultData {
                    let infor = XGL_userDataCacheManager.shared.token
                    infor.token = result.token
                    LoginInfor.share.infor = result
                    UserDefaults.standard.set(result.userId, forKey: XGL_AppInforCacheKey.share.userDefaultsAccountKey)
                    XGL_userDataCacheManager.shared.saveInfor(inforData: infor)
                    
                    XGL_Notice.default.post(name: XGL_Notification.login.notificationName, object: nil)
                }
                MXProgressHUD.hudWithText("Registered successfully ~") {
                    let vc = EditProfileHeaderViewController()
                    self.navigationController?.pushViewController(vc)
                }
                break
            default:
                break
            }
        }
    }
}

extension LoginCreateAccountViewController: CreateAccountDelegate{
    func createAccount() {
        guard self.createView.email.isEmpty == false else {
            MXProgressHUD.hudWithText("Please enter email")
            return
        }
        guard self.createView.password.isEmpty == false else {
            MXProgressHUD.hudWithText("Please enter password")
            return
        }
        guard self.createView.passwordSure.isEmpty == false else {
            MXProgressHUD.hudWithText("Please enter Confirm Password")
            return
        }
        guard self.createView.passwordSure == self.createView.password else {
            MXProgressHUD.hudWithText("Two passwords do not match")
            return
        }
        self.view.addSubview(self.hudView)
        self.hudView.hudView.startAnimation()
        self.hudView.snp.makeConstraints { make in
            make.width.centerX.bottom.equalToSuperview()
            make.top.equalTo(PanNavibarH)
        }
        self.verificationEmailRequest()
//        self.registerEmailRequest()
    }
}
